/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAAbstractFeatureViewButton.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 30/10/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAAbstractFeatureViewButton.h"
#import "BALibrarySettings.h"
#import "InfoGalleriImageUrlUtils.h"
#import "LazyLoadManager.h"

@interface BAAbstractFeatureViewButton () {
    BOOL isLoaded;
}

@property (strong, nonatomic) UIImageView *headerImageView;
@property (strong, nonatomic) UILabel *headerTitleLabel;
@property (strong, nonatomic) UIView *highlightView;
@property (strong, nonatomic) UIActivityIndicatorView *loadingIndicator;

@end

@implementation BAAbstractFeatureViewButton

- (id)init
{
    self = [super init];
    if (self) {
        [self setUpButton];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUpButton];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpButton];
    }
    return self;
}

-(void)setUpButton{
    if (!isLoaded) {
        isLoaded = YES;
        _headerTitleLabel = [UILabel new];
        _headerImageView = [UIImageView new];
        UIView* titleContainerView = [UIView new];
        _highlightView = [UIView new];
        _loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        
        [self.loadingIndicator setHidesWhenStopped:YES];
        [self.loadingIndicator startAnimating];
        
        [self.headerImageView setAlpha:0];
        [self.headerImageView setContentMode:UIViewContentModeScaleAspectFill];
        [self.headerImageView setClipsToBounds:YES];
        [self.headerImageView setUserInteractionEnabled:NO];
        
        [titleContainerView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
        
        [titleContainerView setUserInteractionEnabled:NO];
        
        [self.highlightView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.1]];
        [self.highlightView setHidden:YES];
        [self.highlightView setUserInteractionEnabled:NO];
        
        if (RD_IS_IPAD) {
            [self.headerTitleLabel setFont:BA_FONT_LIGHT(20)];
        } else{
            [self.headerTitleLabel setFont:BA_FONT_MEDIUM(13)];
        }
        
        [self.headerTitleLabel setTextColor:[UIColor whiteColor]];
        [self.headerTitleLabel setBackgroundColor:[UIColor clearColor]];
        [self.headerTitleLabel setNumberOfLines:1];
        
        [self addSubview:self.loadingIndicator];
        [self addSubview:self.headerImageView];
        [self addSubview:titleContainerView];
        [titleContainerView addSubview:self.headerTitleLabel];
        [self addSubview:self.highlightView];
        
        self.loadingIndicator.translatesAutoresizingMaskIntoConstraints = NO;
        self.headerImageView.translatesAutoresizingMaskIntoConstraints = NO;
        titleContainerView.translatesAutoresizingMaskIntoConstraints = NO;
        self.headerTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.highlightView.translatesAutoresizingMaskIntoConstraints = NO;
        
        
        [self addConstraint:[NSLayoutConstraint
                             constraintWithItem:self.loadingIndicator
                             attribute:NSLayoutAttributeCenterX
                             relatedBy:NSLayoutRelationEqual
                             toItem:self
                             attribute:NSLayoutAttributeCenterX
                             multiplier:1.0f
                             constant:0.0f]];
        
        [self addConstraint:[NSLayoutConstraint
                             constraintWithItem:self.loadingIndicator
                             attribute:NSLayoutAttributeCenterY
                             relatedBy:NSLayoutRelationEqual
                             toItem:self
                             attribute:NSLayoutAttributeCenterY
                             multiplier:1.0f
                             constant:0.0f]];
        
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[headerImageView]|" options:0 metrics: 0 views:@{@"headerImageView": self.headerImageView}]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerImageView]|" options:0 metrics: 0 views:@{@"headerImageView": self.headerImageView}]];
        
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[highlightView]|" options:0 metrics: 0 views:@{@"highlightView": self.highlightView}]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[highlightView]|" options:0 metrics: 0 views:@{@"highlightView": self.highlightView}]];
        
        
        if (RD_IS_IPAD) {
            //75
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[titleContainerView]|" options:0 metrics: 0 views:@{@"titleContainerView": titleContainerView}]];
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[titleContainerView(75)]|" options:0 metrics: 0 views:@{@"titleContainerView": titleContainerView}]];
            
            [titleContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[headerTitleLabel]-15-|" options:0 metrics: 0 views:@{@"headerTitleLabel": self.headerTitleLabel}]];
            
            [titleContainerView addConstraint:[NSLayoutConstraint
                                 constraintWithItem:self.headerTitleLabel
                                 attribute:NSLayoutAttributeCenterY
                                 relatedBy:NSLayoutRelationEqual
                                 toItem:titleContainerView
                                 attribute:NSLayoutAttributeCenterY
                                 multiplier:1.0f
                                 constant:0.0f]];
//            [titleContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-9-[headerTitleLabel]-8-|" options:0 metrics: 0 views:@{@"headerTitleLabel": self.headerTitleLabel}]];
        } else{
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[titleContainerView]|" options:0 metrics: 0 views:@{@"titleContainerView": titleContainerView}]];
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[titleContainerView]|" options:0 metrics: 0 views:@{@"titleContainerView": titleContainerView}]];
            
            [titleContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[headerTitleLabel]-15-|" options:0 metrics: 0 views:@{@"headerTitleLabel": self.headerTitleLabel}]];
            [titleContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-9-[headerTitleLabel]-8-|" options:0 metrics: 0 views:@{@"headerTitleLabel": self.headerTitleLabel}]];
        }
        
    }
    
}

-(void)layoutSubviews{
    [self.headerTitleLabel setPreferredMaxLayoutWidth:self.frame.size.width-30];
    [super layoutSubviews];
}

-(void)setHighlighted:(BOOL)highlighted{
    [self.highlightView setHidden:!highlighted];
    [super setHighlighted:highlighted];
}

-(void)setHeaderTitle:(NSString*)title andImageUrl:(NSString*)origUrl
{
    if (RD_IS_IPAD) {
        [self.headerTitleLabel setText:title.uppercaseString];
    } else{
        [self.headerTitleLabel setText:title];
    }
    
    if (origUrl.length > 0) {
        NSString* resize_url;
        if (RD_IS_IPAD) {
            resize_url = [InfoGalleriImageUrlUtils getResizedImageUrl:origUrl toWidth:683 toHeight:500 usingQuality:7 withMode:IMAGE_URL_UTILS_RESIZE_MODE_BOUNDING_BOX outputFormat:@"jpeg" doubleUpForRetina:YES];
        } else{
            resize_url = [InfoGalleriImageUrlUtils getResizedImageUrl:origUrl toWidth:320 toHeight:180 usingQuality:7 withMode:IMAGE_URL_UTILS_RESIZE_MODE_CROP_EDGES outputFormat:@"jpeg" doubleUpForRetina:YES];
        }
        
        
        [[LazyLoadManager instance] lazyLoadUIImageFromUrl:resize_url withOwner:self setScaleforRetina:YES onSuccess:^(UIImage *image) {
            [self.headerImageView setImage:image];
            [UIView animateWithDuration:0.25 animations:^{
                [self.headerImageView setAlpha:1];
            } completion:^(BOOL finished) {
                [self.loadingIndicator stopAnimating];
            }];
        } onError:^(LazyLoaderErrorCode errorCode, NSString *errorMessage) {
            [self.loadingIndicator stopAnimating];
        }];
    } else{
        [self.loadingIndicator stopAnimating];
    }
    
}

@end
